package demo2;

class Animal {
    protected String name;
    protected int age;

    public void eat(){
        System.out.println(name + "正在吃饭");
    }
}

class Dog extends Animal {
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public void eat(){
        System.out.println(name + "正在吃狗粮");
    }
}

class Cat extends Animal {
    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public void eat(){
        System.out.println(name + "正在吃猫粮");
    }
}

public class Test {
    public static void eat(Animal animal){
        if(animal instanceof Dog){
            ((Dog)animal).eat();
        }else if(animal instanceof  Cat){
            ((Cat)animal).eat();
        }else{
            System.out.println("nothing");
        }
    }
    public static void main(String[] args) {
        Dog dog = new Dog("小黑",3);
        Cat cat = new Cat("小花",2);
        eat(dog);
        eat(cat);
    }
}
